blockedPlayer = {} --block loadcar
--countVeh = 1
loadVehTime = 120 --sec

function quitPlayer(quitType)
	blockedPlayer[source] = nil
end
addEventHandler("onPlayerQuit", getRootElement(), quitPlayer)

function joinPlayer()
	blockedPlayer[source] = true
end
addEventHandler("onPlayerJoin", getRootElement(), joinPlayer)

function resStart(res)
	local players = getElementsByType("player")
	for k, player in ipairs(players) do
		blockedPlayer[player] = true
	end
end
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), resStart)

function saveVehicle(player, commandName)
	local namePlayer = getPlayerName(player)
	if (namePlayer) then
		local vehicle = getPedOccupiedVehicle(player)
		if (vehicle) then
			local healthVeh = getElementHealth(vehicle)
			if (tonumber(healthVeh) > 249.9999923706054) then
				-- create xml node
				local xmlRootNode = xmlCreateFile("saves/"..namePlayer..".dbc", "savecar")
				if (xmlRootNode) then
					xmlNodeSetAttribute(xmlRootNode, "user", namePlayer)
					local xmlModelNode = xmlCreateChild(xmlRootNode, "model")
					xmlNodeSetAttribute(xmlModelNode, "ID", getElementModel(vehicle))
					xmlNodeSetAttribute(xmlModelNode, "name", getVehicleName(vehicle))
					xmlNodeSetAttribute(xmlModelNode, "health", healthVeh)
					--
					local xmlColorNode = xmlCreateChild(xmlRootNode, "color")
					local color1, color2, color3, color4 = getVehicleColor(vehicle)
					xmlNodeSetAttribute(xmlColorNode, "color1", color1)
					xmlNodeSetAttribute(xmlColorNode, "color2", color2)
					xmlNodeSetAttribute(xmlColorNode, "color3", color3)
					xmlNodeSetAttribute(xmlColorNode, "color4", color4)
					xmlNodeSetAttribute(xmlColorNode, "paintjob", getVehiclePaintjob(vehicle))
					--
					local xmlPanelNode = xmlCreateChild(xmlRootNode, "panelState")
					for panel=0, 6 do
						local xmlPanelNumberNode = xmlCreateChild(xmlPanelNode, "panel"..tostring(panel))
						xmlNodeSetAttribute(xmlPanelNumberNode, "value", getVehiclePanelState(vehicle, panel))
					end
					--doorState
					local xmlDoorNode = xmlCreateChild(xmlRootNode, "doorState")
					for door=0, 5 do
						local xmlDoorNumberNode = xmlCreateChild(xmlDoorNode, "door"..tostring(door))
						xmlNodeSetAttribute(xmlDoorNumberNode, "value", getVehicleDoorState(vehicle, door))
					end
					--wheelState
					local xmlWheelNode = xmlCreateChild(xmlRootNode, "wheelState")
					local frontLeft, frontRight, rearLeft, rearRight = getVehicleWheelStates(vehicle)
					xmlNodeSetAttribute(xmlWheelNode, "frontLeft", frontLeft)
					xmlNodeSetAttribute(xmlWheelNode, "frontRight", frontRight)
					xmlNodeSetAttribute(xmlWheelNode, "rearLeft", rearLeft)
					xmlNodeSetAttribute(xmlWheelNode, "rearRight", rearRight)
					--
					local xmlUpgradeNode = xmlCreateChild(xmlRootNode, "upgrade")
					local upgrades = getVehicleUpgrades(vehicle)
					for upgradeKey, upgradeValue in ipairs(upgrades) do
						local xmlUpgradeIdNode = xmlCreateChild(xmlUpgradeNode, "upgrade"..tostring(upgradeKey-1))
						xmlNodeSetAttribute(xmlUpgradeIdNode, "name", getVehicleUpgradeSlotName(upgradeKey-1))
						xmlNodeSetAttribute(xmlUpgradeIdNode, "value", upgradeValue)
					end
					--
					xmlSaveFile(xmlRootNode)
					outputChatBox("*"..namePlayer.." have saved a car!", player, 0, 255, 0)
				else
					outputChatBox("*Error creating "..namePlayer.." xml!", player, 255, 0, 0)
				end
			else
				outputChatBox("*Your car now explode!", player, 255, 0, 0)
			end
		else
			outputChatBox("*Where your car? You must be inside the vehicle.", player, 255, 0, 0)
		end
	end
end

function loadVehicle(player, commandName)
	if (blockedPlayer[player]) then
		local namePlayer = getPlayerName(player)
		if (namePlayer) then
			-- load xmlNode
			local xmlRootNode = xmlLoadFile("saves/"..namePlayer..".dbc")
			--outputChatBox(tostring(xmlRootNode), player) --
			if (xmlRootNode) then
				local px, py, pz = getElementPosition(player)
				--
				local xmlModelNode = xmlFindChild(xmlRootNode, "model", 0)
				local model = xmlNodeGetAttribute(xmlModelNode, "ID")
				local vehicle = createVehicle(model, px+5, py, pz+0.5)
				--
				local xmlColorNode = xmlFindChild(xmlRootNode, "color", 0)
				local paintjob = xmlNodeGetAttribute(xmlColorNode, "paintjob")
				setVehiclePaintjob(vehicle, paintjob)
				local col1 = tonumber(xmlNodeGetAttribute(xmlColorNode, "color1"))
				local col2 = tonumber(xmlNodeGetAttribute(xmlColorNode, "color2"))
				local col3 = tonumber(xmlNodeGetAttribute(xmlColorNode, "color3"))
				local col4 = tonumber(xmlNodeGetAttribute(xmlColorNode, "color4"))
				--outputChatBox("Color: "..tostring(col1).." "..tostring(col2).." "..tostring(col3).." "..tostring(col4), player)
				setVehicleColor(vehicle, col1, col2, col3, col4)
				--
				local xmlUpgradeNode = xmlFindChild(xmlRootNode, "upgrade", 0)
				--local upgrades = getVehicleUpgrades(vehicle)
				for slot=0, 16 do
					local xmlUpgradeIdNode = xmlFindChild(xmlUpgradeNode, "upgrade"..tostring(slot), 0)
					--outputChatBox(tostring(xmlUpgradeIdNode), player) --
					if (xmlUpgradeIdNode) then
						local upgrade = xmlNodeGetAttribute(xmlUpgradeIdNode, "value")
						addVehicleUpgrade(vehicle, upgrade)
					end
				end
				--
				local xmlPanelNode = xmlFindChild(xmlRootNode, "panelState", 0)
				for panelID=0, 6 do
					local xmlPanelNumberNode = xmlFindChild(xmlPanelNode, "panel"..tostring(panelID), 0)
					local state = xmlNodeGetAttribute(xmlPanelNumberNode, "value")
					setVehiclePanelState(vehicle, panelID, state)
				end
				--
				local xmlDoorNode = xmlFindChild(xmlRootNode, "doorState", 0)
				for door=0, 5 do
					local xmlDoorNumberNode = xmlFindChild(xmlDoorNode, "door"..tostring(door), 0)
					local state = xmlNodeGetAttribute(xmlDoorNumberNode, "value")
					setVehicleDoorState(vehicle, door, state)
				end
				--
				local xmlWheelNode = xmlFindChild(xmlRootNode, "wheelState", 0)
				local frontLeft = xmlNodeGetAttribute(xmlWheelNode, "frontLeft")
				local frontRight = xmlNodeGetAttribute(xmlWheelNode, "frontRight")
				local rearLeft = xmlNodeGetAttribute(xmlWheelNode, "rearLeft")
				local rearRight = xmlNodeGetAttribute(xmlWheelNode, "rearRight")
				setVehicleWheelStates(vehicle, frontLeft, rearLeft, frontRight, rearRight)
				--health
				local health = xmlNodeGetAttribute(xmlModelNode, "health")
				--outputChatBox("Health "..tostring(health), player) --
				setElementHealth(vehicle, health)
				--
				xmlUnloadFile(xmlRootNode)
				outputChatBox("*This your car!", player, 0, 255, 0)
				blockedPlayer[player] = false
				setTimer(blockPlayer, loadVehTime*1000, 1, player)
			else
				outputChatBox("*Where your car? First you need to save the car before to load this.", player, 255, 0, 0)
			end
		end
	else
		outputChatBox("*You have to wait "..loadVehTime.." seconds before load vehicle.", player, 255, 0, 0)
	end
end

function blockPlayer(player)
	blockedPlayer[player] = true
end

addCommandHandler("savecar", saveVehicle)
addCommandHandler("loadcar", loadVehicle)